{% extends "base.html" %}

{% block title %}聊天 - SteHub{% endblock %}

{% block extra_css %}
<style>
.chat-container {
    height: 70vh;
    border: 1px solid #dee2e6;
    border-radius: 8px;
    overflow: hidden;
}
.chat-sidebar {
    background: #f8f9fa;
    border-right: 1px solid #dee2e6;
}
.chat-main {
    display: flex;
    flex-direction: column;
}
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}
.chat-input {
    border-top: 1px solid #dee2e6;
    padding: 1rem;
}
.online-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #28a745;
    display: inline-block;
    margin-right: 5px;
}
.room-card {
    transition: all 0.2s ease;
}
.room-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-comment-dots me-2"></i>聊天室</h2>
            <a href="{{ url_for('chat.create_room') }}" class="btn btn-primary">
                <i class="fas fa-plus me-2"></i>创建聊天室
            </a>
        </div>
    </div>
</div>

<div class="row">
    <!-- 我加入的聊天室 -->
    <div class="col-md-6">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="fas fa-users me-2"></i>我加入的聊天室</h5>
                <span class="badge bg-primary">{{ user_rooms|length }}</span>
            </div>
            <div class="card-body">
                {% if user_rooms %}
                    <div class="row">
                        {% for room in user_rooms %}
                        <div class="col-lg-6 mb-3">
                            <div class="card room-card h-100">
                                <div class="card-body">
                                    <h6 class="card-title">{{ room.name }}</h6>
                                    <p class="card-text small text-muted">
                                        {{ room.description or '暂无描述' }}
                                    </p>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <small class="text-muted">
                                            <i class="fas fa-user me-1"></i>{{ room.member_count }}
                                        </small>
                                        <div>
                                            <a href="{{ url_for('chat.view_room', room_id=room.id) }}" 
                                               class="btn btn-sm btn-primary">
                                                进入
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-4">
                        <i class="fas fa-comments fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">还没有加入任何聊天室</h5>
                        <p class="text-muted">加入一个公共聊天室或创建自己的聊天室</p>
                        <a href="{{ url_for('chat.create_room') }}" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>创建聊天室
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 公共聊天室 -->
    <div class="col-md-6">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="fas fa-globe me-2"></i>公共聊天室</h5>
                <span class="badge bg-success">{{ public_rooms|length }}</span>
            </div>
            <div class="card-body">
                {% if public_rooms %}
                    <div class="list-group list-group-flush">
                        {% for room in public_rooms %}
                        <div class="list-group-item d-flex justify-content-between align-items-center">
                            <div class="flex-grow-1">
                                <h6 class="mb-1">{{ room.name }}</h6>
                                <p class="mb-1 small text-muted">{{ room.description or '暂无描述' }}</p>
                                <small class="text-muted">
                                    <i class="fas fa-user me-1"></i>{{ room.member_count }} 成员
                                    {% if room.max_members %}
                                    / 最多 {{ room.max_members }} 人
                                    {% endif %}
                                </small>
                            </div>
                            <div class="flex-shrink-0">
                                {% if room in user_rooms %}
                                <span class="badge bg-success me-2">已加入</span>
                                {% else %}
                                <form method="POST" action="{{ url_for('chat.join_room', room_id=room.id) }}" 
                                      class="d-inline join-room-form">
                                    <button type="submit" class="btn btn-sm btn-outline-primary">
                                        加入
                                    </button>
                                </form>
                                {% endif %}
                                <a href="{{ url_for('chat.view_room', room_id=room.id) }}" 
                                   class="btn btn-sm btn-primary ms-1">
                                    查看
                                </a>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-4">
                        <i class="fas fa-comments fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无公共聊天室</h5>
                        <p class="text-muted">创建第一个公共聊天室吧！</p>
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- 在线用户 -->
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-user-clock me-2"></i>在线用户</h5>
            </div>
            <div class="card-body">
                {% if online_users %}
                    <div class="d-flex flex-wrap gap-2">
                        {% for user in online_users %}
                        <div class="position-relative">
                            <a href="{{ url_for('chat.private_chat', user_id=user.id) }}" 
                               class="btn btn-outline-success btn-sm">
                                <span class="online-indicator"></span>
                                {{ user.username }}
                            </a>
                        </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <p class="text-muted text-center mb-0">暂无在线用户</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 快速操作 -->
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <h5 class="mb-3">快速操作</h5>
                <div class="row">
                    <div class="col-md-3 text-center">
                        <a href="{{ url_for('chat.create_room') }}" class="btn btn-outline-primary w-100 mb-2">
                            <i class="fas fa-plus fa-2x mb-2"></i><br>
                            创建房间
                        </a>
                    </div>
                    <div class="col-md-3 text-center">
                        <a href="#" class="btn btn-outline-success w-100 mb-2" id="findRandomRoom">
                            <i class="fas fa-random fa-2x mb-2"></i><br>
                            随机加入
                        </a>
                    </div>
                    <div class="col-md-3 text-center">
                        <a href="#" class="btn btn-outline-info w-100 mb-2" id="refreshRooms">
                            <i class="fas fa-sync-alt fa-2x mb-2"></i><br>
                            刷新列表
                        </a>
                    </div>
                    <div class="col-md-3 text-center">
                        <a href="#" class="btn btn-outline-warning w-100 mb-2">
                            <i class="fas fa-cog fa-2x mb-2"></i><br>
                            聊天设置
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 加入房间表单
    const joinRoomForms = document.querySelectorAll('.join-room-form');
    joinRoomForms.forEach(form => {
        form.addEventListener('submit', function(e) {
            e.preventDefault();
            
            fetch(this.action, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    if (data.redirect_url) {
                        window.location.href = data.redirect_url;
                    } else {
                        location.reload();
                    }
                } else {
                    alert('加入失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('加入失败');
            });
        });
    });

    // 随机加入房间
    document.getElementById('findRandomRoom').addEventListener('click', function(e) {
        e.preventDefault();
        
        // 从公共房间中随机选择一个
        const publicRooms = {{ public_rooms|tojson }};
        if (publicRooms.length > 0) {
            const randomRoom = publicRooms[Math.floor(Math.random() * publicRooms.length)];
            window.location.href = "{{ url_for('chat.view_room', room_id=0) }}".replace('0', randomRoom.id);
        } else {
            alert('暂无公共聊天室可供加入');
        }
    });

    // 刷新房间列表
    document.getElementById('refreshRooms').addEventListener('click', function(e) {
        e.preventDefault();
        location.reload();
    });

    // 初始化Socket.IO连接
    if (typeof io !== 'undefined') {
        const socket = io();
        
        // 监听用户状态变化
        socket.on('user_online', function(data) {
            console.log('用户上线:', data.username);
            // 可以在这里更新在线用户列表
        });
        
        socket.on('user_offline', function(data) {
            console.log('用户下线:', data.username);
            // 可以在这里更新在线用户列表
        });
    }
});
</script>
{% endblock %}